define(function () {
    'use strict';

    function ctrl($scope, $ionicNavBarDelegate, $stateParams, $timeout, $rootScope, $ionicSlideBoxDelegate,
                  LocalCacheService, CommonService, WebService, InfoService, TdxLocalFuncService, $state, $location, $window, $ionicLoading) {
        var par = $scope.param = {};
        var isEnable;
        var timer;
        $scope.doRefresh = function () {
            par.ads = null;
            par.ads_login = null;
            par.botads = null;
            par.funds = null;
            par.products = null;
            initialize();
            $scope.$broadcast("scroll.refreshComplete");
        };
        $scope.$on('$ionicView.beforeEnter', function () {
            par.dev_width = $window.innerWidth;
            LocalCacheService.setAppContextName($stateParams["app_context_name"]);
            $scope.app_context_name = LocalCacheService.getAppContextName();
            par.recommendInfos = $stateParams["recommendInfos"];
            LocalCacheService.setRecommendInfos(par.recommendInfos);
            par.tf_op_station = $stateParams["tf_op_station"]; //打开app
            par.notice_content = '';
            par.backButtonPressedOnceToExit = false;
            initParamsAndProList();
            initNotice();
            initialize();
        });
        $scope.$on('refresh', function(evt, current, next) {
            if(ionic.Platform.isIOS()){
                initialize();
            }
        });
        //首页双击退出
        $scope.onBackKeyDown = function () {
            if (par.backButtonPressedOnceToExit) {
                ionic.Platform.exitApp();
            } else {
                par.backButtonPressedOnceToExit = true;
                CommonService.showLogout({message: '再按一次退出理财'});
                $timeout(function () {
                    par.backButtonPressedOnceToExit = false;
                }, 2000);
            }
        };
        $scope.thsExitModle = function () {
            tf.otherService.exitModule({moduleID: 'gcs_fin'});
            return;
        };
        $scope.go = function (url) {
            if (isEnable) {
                if (url == 'tab.account-index') {
                    $state.go(url);
                } else if (url == '#/tab/account-assets-index') {
                    $scope.goTo('tab.account-assets-index', null, null);
                } else {
                    $location.url(url.substring(1));
                }
            }
        };
        $scope.goTo = function (url, par1, par2) {
            if (isEnable) {
                if (par1 || par2) {
                    $state.go(url, {id: par1, index: par2});
                } else {
                    if (TdxLocalFuncService.isTDX()) {
                        TdxLocalFuncService.tdxTradeLocked().then(function (data) {
                            if (data.text == '1' || data.text == 1) {
                                return;
                            } else {
                                $state.go(url)
                            }
                        });
                    } else {
                        $state.go(url)
                    }
                }
            }
        };
        $scope.slideHandle = function (index) {
            var count = $ionicSlideBoxDelegate.$getByHandle('slideboximgs').slidesCount();
            if (timer) {
                $timeout.cancel(timer);
            }
            if (index == count - 1) {
                timer = $timeout(function () {
                    $ionicSlideBoxDelegate.$getByHandle('slideboximgs').slide(0);
                }, 5000);
            } else if (index == 0) {
                timer = $timeout(function () {
                    $ionicSlideBoxDelegate.$getByHandle('slideboximgs').next();
                }, 5000);
            }
        };
        $scope.repeatFinish = function () {
            $ionicSlideBoxDelegate.$getByHandle('slideboximgs').update();
            $ionicSlideBoxDelegate.$getByHandle('slideboximgs').slide(0);
        };

        function initParamsAndProList() {
            if (!CommonService.isStrEmpty(par.tf_op_station) && par.tf_op_station.indexOf("]") != -1) {
                var ops = par.tf_op_station.split("[");
                var uuid = ops[ops.length - 1].split("]")[0];
                LocalCacheService.set("UUID", uuid);
                LocalCacheService.set("op_station", par.tf_op_station);
            }
            if (par.equityFund || par.currencyFund) {
                return;
            }
            InfoService.getProductsList().then(function (data) {
                par.equityFund = data.equityFund;
                par.currencyFund = data.currencyFund;
            })
        }

        function initNotice() {
            WebService.getNotice("system").then(
                function (notice) {
                    if (notice.error_no == 0 && notice.isColsed == 1) { //系统关闭公告
                        WebService.qryProdWhite().then(function (data) {
                            if (!data || !data.isProdWhite) {
                                par.notice_content = notice.content;
                                $ionicNavBarDelegate.showBar(false);
                                $rootScope.hideTabs = true;
                                $scope.exitApp = function () {
                                    navigator.app.exitApp();
                                };
                            }
                        });
                    } else {
                        //公告获取
                        WebService.getNotice("index").then(function (notice) {
                            if (notice.error_no == 0 && notice.isColsed != 1 && notice.title && notice.content) {
                                CommonService.showNotice(notice.title + "&&" + notice.content, "tf_system_notice_show" + notice.updateTime);
                            }
                        });
                    }
                }
            );

            $timeout(function () {
                if (navigator && navigator.splashscreen) {
                    navigator.splashscreen.hide();
                }
            }, 1000);
        }

        function iniAdsProducts() {
            if (LocalCacheService.getUser()) {
                par.ads = null;
                par.botads = null;
                if (par.ads_login || par.products || par.funds) {
                    return;
                }
                WebService.wtAd("gcs_fin_index_1").then(function (data) {
                    if (data && data.length > 0) {
                        par.ads_login = data;
                    }
                });
                InfoService.getProductsByTheme('sliding', 0, null).then(function (data) {
                    if (data && data.length > 0) {
                        par.products = data;
                        par.style = {'width': (data.length * 165 + 5) + 'px'};
                        for (var i = 0; i < data.length; i++) {
                            if ("0,3,4".indexOf(par.products[i].prod_status) > -1) {
                                if ("1" == par.products[i].otc_status) {
                                    par.products[i].sellStatus = 1; // 购买
                                } else if ("2,3".indexOf(par.products[i].otc_status) > -1) {
                                    par.products[i].sellStatus = 3; // 已售罄
                                } else {
                                    par.products[i].sellStatus = 2; //未到销售时间
                                }
                            } else if ("2" == par.products[i].prod_status) {
                                par.products[i].sellStatus = 4; //暂停申购
                            } else {
                                if ("2,3".indexOf(par.products[i].otc_status) > -1) {
                                    par.products[i].sellStatus = 3;// 已售罄
                                } else {
                                    par.products[i].sellStatus = 5; //暂停交易
                                }
                            }
                        }
                    }
                });
                InfoService.getProductsByTheme('assets', 0, null).then(function (data) {
                    if (data && data.length > 0) {
                        par.funds = data;
                    }
                });
            } else {
                par.funds = null;
                par.products = null;
                par.ads_login = null;
                if (par.ads || par.botads) {
                    return;
                }
                WebService.wtAd("gcs_fin_index_1").then(function (data) {
                    if (data && data.length > 0) {
                        par.ads = data;
                    }
                });
                WebService.wtAd("gcs_fin_index_3").then(function (data) {
                    if (data && data.length > 0) {
                        par.botads = data[0];
                    }
                });
            }
        }

        function checkUser() {
            isEnable = false;
            $ionicLoading.show({noBackdrop:true});
            TdxLocalFuncService.getActivePtUser().then(function (data) {
                TdxLocalFuncService.loginTap(data, function () {
                    isEnable = true;
                    $ionicLoading.hide();
                    iniAdsProducts();
                }, function () {
                    isEnable = true;
                    $ionicLoading.hide();
                    iniAdsProducts();
                });
            }, function () {
                LocalCacheService.removeUser();
                isEnable = true;
                $ionicLoading.hide();
                iniAdsProducts();
            });
        }

        function refresh() {
            TdxLocalFuncService.tdxRefreshPage().then(function () {
                checkUser();
                refresh();
            }, function () {
                iniAdsProducts();
            });
        }

        function initialize() {
            if (TdxLocalFuncService.isTDX()) {
                checkUser();
                refresh();
            } else {
                iniAdsProducts();
                isEnable = true;
            }
        }
    }

    ctrl.$inject = ['$scope', '$ionicNavBarDelegate', '$stateParams', '$timeout', '$rootScope', '$ionicSlideBoxDelegate',
        'LocalCacheService', 'CommonService', 'WebService', 'InfoService', 'TdxLocalFuncService', '$state', '$location', '$window','$ionicLoading'];
    return ctrl;
});